#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    int maxEvents(vector<vector<int>>& events) {
        sort(events.begin(), events.end());
        priority_queue<int, vector<int>, greater<int>> heap;
        int day = 1, attend = 0;
        int i = 0;
        while (i < events.size() || heap.size())
        {
            while (heap.size() && heap.top() < day) heap.pop();
            while (i < events.size() && events[i][0] <= day)
            {
                heap.push(events[i][1]);
                ++i;
            }
            if (heap.size())
            {
                ++attend;
                heap.pop();
            }
            ++day;
        }
        return attend;
    }
};